// pages/Goods/Comment/index.js
const common = require('../../../public.js')
const utils = require('../utils.js')
const config = require('../../../config')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        commentList: [],
        isFyApp: config.isFyApp,
        storeId: '',
        commnet: [],
        commentTotalCount: '',
        showComment: false,
        commentValue: '',
        pageNum: 1,
        pageSize: 10,
        isLoadingBusy: false,
        loadFinished: false,
        active: 3,
        userInfo: null,
        textareaForce: false,
        pubUserId: '',
        commentId: '',
        commentPlachold: '我来说几句',
        focus: false,
        articleDetail: {},
        isIphoneX: null,
        dealStyle: "",
        Badwords:['妈', 'sb', 'SB', '爸', '国', '党', '傻']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this
        this.data.storeId = options.id ? options.id : config.id
        this.data.moduleType = options.moduleType ? options.moduleType : config.type
        common.getLocalUserInfo(function (success, data) {
            if (success) {
                that.data.token = data.token
                that.data.openid = data.openid
                that.setData({
                    userInfo: data.user
                }, function () {
                    // 获取商品评论
                    that.getCommentList(true)
                    const app = getApp()
                    let modelmes = wx.getStorageSync('modelmes');
                    let isIphoneX = app.globalData.isIphoneX;
                    console.log('isIphoneX为', isIphoneX);

                    if (that.data.isFyApp) {// 是飞羊精选，底部显示导航条
                        this.setData({
                            dealStyle: isIphoneX == true ? '168rpx' : '100rpx'// 是苹果x以上机型高度就为100rpx+68rpx,否则为100rpx，100rpx就是底部tabbar的高度
                        })
                    } else {// 不是飞羊精选，底部就不显示导航条
                        this.setData({
                            dealStyle: isIphoneX == true ? '68rpx' : '0rpx'
                        })
                    }
                    this.setData({
                        isIphoneX: isIphoneX
                    })
                })
            }
        }, false)


    },
    textareaBindblur: function () {
        this.setData({
            textareaForce: false,
            commentPlachold: '我来说几句'
        })
    },
    textareaBindfocus: function () {
        console.log('textareaBindfocus---')
        this.setData({
            textareaForce: true,
        })
    },
    onChangeTabbar(e) { //切换底部导航
        const index = e.detail.index
        const url = e.detail.url
        if (index != this.data.active) {
            utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
        }
    },
    //获取评论列表
    getCommentList(refresh = false) {
        console.log('执行拉');
        // if (this.data.isLoadingBusy === true || this.data.loadFinished === true) {
        //   return
        // }
        console.log('这儿呢');
        this.setData({
            isLoadingBusy: true
        })
        const pageNum = refresh ? 1 : this.data.pageNum + 1

        const that = this
        const tempJson = {
            storeId: that.data.storeId,
            pageNum,
            pageSize: that.data.pageSize,
            userId: that.data.userInfo.id,
            token: that.data.token
        }
      
        common.getJson('/scenicQanswer/getScenicQanswerListForUser', tempJson, function (res) {
            if (res.status === 200) {
               
                res.data.qaList.map((item, index) => {
                    let result = common.filter_method(res.data.qaList[index].content,that.data.Badwords)
                    res.data.qaList[index].content = result
        })
                that.data.pageNum = pageNum
                const finshed = res.data.qaList === null || res.data.qaList.length === 0 ? true : false
                that.setData({
                    commentTotalCount: res.data.qaCount,
                    commentList: res.data.qaList,
                    // commnet: refresh ? res.data : [...that.data.commnet, ...res.data],
                    loadFinished: finshed
                })
            } else {
                that.setData({
                    loadFinished: true
                })
            }
        }, function () {
            that.setData({
                isLoadingBusy: false
            })
        })
    },
    loadMore() {
        this.getCommentList(false)
    },
    commentSave() {
        if (common.isEmptyString(this.data.commentValue)) {
            common.showToast('您没有输入任何内容')
            return
        }
        if(common.filter_methods(this.data.commentValue,this.data.Badwords)){
            common.showToast('含有违规词，请重新输入')
            return
        }
        console.log('commentIdwei', this.data.commentId);
        if (this.data.commentId) { // 有commentid是二级评论
            this.replyComment()
            console.log('回复')
        } else {
            this.commentFunc()
            console.log('评论---')
        }
    },
    clickReply(e) { // 点击评论中的回复
        let that = this
        console.log('回复Ta--', e);
        let {
            pubuserid,
            commentid,
            pubusername
        } = e.currentTarget.dataset
        that.setData({
            pubUserId: pubuserid,
            commentId: commentid,
            commentPlachold: '@' + pubusername + '：'
        })
    },
    onChange(e) {
        let {
            value
        } = e.detail

        this.setData({
            commentValue: value
        })
    },

    commentFunc() {
        const that = this
        const tempJson = {
            token: that.data.token,
            userId: that.data.userInfo.id,
            storeId: that.data.storeId,
            content: that.data.commentValue
        }
        common.post('/scenicQanswer/saveScenicQanswer', tempJson,
            function (res) {
                if (res.status === 200) {

                    that.clearReplyInfo()
                    that.getCommentList(true)


                } else {
                    common.showRequestErrMsg(res)
                }
            },
            function () {
                that.setData({
                    isLoadingBusy: false
                })
            })
    },

    replyComment() { // 初始化店铺信息
        const that = this
        const tempJson = {
            token: that.data.token,
            userId: that.data.userInfo.id,
            qanswerId: that.data.commentId,// 问答ID
            pubUserId: that.data.userInfo.id,// 回复人ID
            receiverUserId: that.data.pubUserId,// 被回复人ID
            content: that.data.commentValue
        }
        common.post('/scenicQanswer/saveScenicQanswerChat', tempJson,
            function (res) {
                if (res.status === 200) {
                    that.clearReplyInfo()
                    that.getCommentList(true)
                } else {
                    common.showRequestErrMsg(res)
                }
            },
            function () {
                that.setData({
                    isLoadingBusy: false
                })
            })
    },

    clearReplyInfo() {
        this.setData({
            commentValue: '',
            pubUserId: "",
            commentId: '',
            commentPlachold: '我来说几句'
        })
    },
    praiseAction(e) {
        const id = e.currentTarget.dataset.item.id
        const userPraiseFlag = e.currentTarget.dataset.item.userPraiseFlag
        if (userPraiseFlag) {
            console.log('取消点赞')
            this.cancleCommentPraise(id)
        } else {
            console.log('点赞')
            this.commitPraise(id)
        }
    },

    cancleCommentPraise(id) {
        let that = this
        let tempJson = {
            userId: (that.data.userInfo && that.data.userInfo.id) ? that.data.userInfo.id : '',
            token: that.data.token,
            qanswerId: id
        }
        common.post('/scenicQanswer/cancleScenicQanswerPraise', tempJson, function (res) {
            if (res.status === 200) {
                that.getCommentList(true)
            }
        })
    },
    commitPraise(id) { // 点赞
        const that = this
        let tempJson = {
            userId: that.data.userInfo.id,
            token: that.data.token,
            qanswerId: id
        }
        common.post('/scenicQanswer/saveScenicQanswerPraise', tempJson, function (res) {
            if (res.status == 200) {
                that.getCommentList(true)
            }
        })
    },

    //获取全部列表
    getAllComment() {
        wx.navigateTo({
            url: '/pages/ScenicArea/ScenicAllQuestion/index?id=' + this.data.storeId,
        })
    },
})